set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
/* *******************************************************************
** DATE :
** Writer :
** Memo :
******************************************************************** */
CREATE PROCEDURE [dbo].[TSP_Manager_GetInfo]
(
	@IsTransaction	tinyint			= 0
,	@ErrorCode		int				= 0		OUTPUT
,	@ErrorMessage	nvarchar( 1000 )= ''	OUTPUT

,	@n4ManagerSN	int		= NULL	OUTPUT
,	@strManagerID	varchar(20)	= NULL	OUTPUT
,	@strManagerName	nvarchar(50)	= NULL	OUTPUT
,	@IsManager	tinyint	= null output
,	@strManagerPasshash	varchar(200)	= NULL	OUTPUT
)
as
Set NOCOUNT ON

-- Frk Default Value
DECLARE @FrkDateNow DateTime
DECLARE @FrkRowCount int
DECLARE @FrkTranCount int
DECLARE @FrkErrorCode int
DECLARE @FrkErrorMessage nvarchar( 1000 )
Set @FrkDateNow = GetDate()
set @FrkTranCount = @@TRANCOUNT
-- Frk Check


If ( @IsTransaction = 0 )
Begin
	If ( @FrkTranCount < 1 )
	Begin
		Set @FrkErrorCode = -1
		Set @FrkErrorMessage = 'Transaction.'
		goto ERROR
	End
	Else If ( @FrkTranCount > 1 )
	Begin
		Set @FrkErrorCode = -2
		Set @FrkErrorMessage = 'Transaction.'
		goto ERROR
	End
End
Else
Begin
	If ( @FrkTranCount <> 0 )
	Begin
		Set @FrkErrorCode = -3
		Set @FrkErrorMessage = 'Transaction.'
		goto ERROR
	End
	Begin Tran
End

If @n4ManagerSN is null and @strManagerID is null
begin
		Set @FrkErrorCode = -10
		Set @FrkErrorMessage = 'key empty.'
		goto ERROR
end

-- Implementation
If @n4ManagerSN is not null and @n4ManagerSN > 0
begin
	SELECT
		@strManagerID = strManagerID
	,	@strManagerPasshash = strManagerPasshash
	,	@strManagerName = strManagerName
	,	@IsManager = IsManager
	FROM
		dbo.TB_Manager WITH (NOLOCK)
	WHERE
		n4ManagerSN = @n4ManagerSN
end
else if @strManagerID is not null 
begin 
	SELECT
		@n4ManagerSN = n4ManagerSN
	,	@strManagerPasshash = strManagerPasshash
	,	@strManagerName = strManagerName
	,	@IsManager = IsManager
	FROM
		dbo.TB_Manager WITH (NOLOCK)
	WHERE
		strManagerID = @strManagerID
end
else if @strManagerName is not null 
begin
	SELECT
		@n4ManagerSN = n4ManagerSN
	,	@strManagerPasshash = strManagerPasshash
	,	@strManagerName = strManagerName
	,	@IsManager = IsManager
	FROM
		dbo.TB_Manager WITH (NOLOCK)
	WHERE
		strManagerName = @strManagerName
end

-- Frk Error Check
SELECT
	@FrkErrorCode = @@ERROR
,	@FrkRowCount = @@RowCount

-- End

COMPLETE:
	If ( @IsTransaction <> 0 )
	Begin
		If ( @@TRANCOUNT = 1 )
		Begin
			Commit Tran
		End
	End
	SET @ErrorCode = 0
	SET @ErrorMessage = ''
	return 0
ERROR:
	If ( @IsTransaction <> 0 )
	Begin
		If ( @@TRANCOUNT = 1 )
		Begin
			Rollback Tran
		End
	End
	SET @ErrorCode = @FrkErrorCode
	SET @ErrorMessage = @FrkErrorMessage
	return @FrkErrorCode
